#include "Xavi.h"

#define player_guid_call_offest 0x526930
#define player_base_call_offest 0x3FCB30
#define  player_base_this_offest 0xBE202C

ULONG GetGuidCall=0;
ULONG pGetBaseCall=0;
ULONG pGetBasethis =0;

OBJECTGUID GetPlayerGUID()
{
	char szPlayer[]="player";
	ULONG PlayerPtr = (ULONG) szPlayer;
	OBJECTGUID objGUID;
	__asm
	{
		pushad
		mov		eax,PlayerPtr
		push		eax
		call		GetGuidCall
		mov		objGUID.hDword,edx
		mov		objGUID.lDword, eax
		add		esp,4
		popad
	}
	return objGUID;
}
DWORD __stdcall GetPlayerBase(OBJECTGUID objGUID)
{
	OBJECTGUID tmpobjGUID = objGUID;
	OBJECTGUID* pobjGUID = &tmpobjGUID;
	DWORD pBase=0;
	__asm
	{
		pushad
		push		pobjGUID
		push		objGUID.lDword
		mov		ecx,	pGetBasethis
		mov		ecx, dword ptr [ecx]
		call		pGetBaseCall
		mov		pBase,	eax
		popad
	}
	return pBase;
}
COORDINATE __stdcall GetPlayerCoord(DWORD pBase)
{
	COORDINATE tmpCoord;
	__asm
	{
		pushad
		mov		eax,	pBase
		mov		eax, dword ptr [eax+0xe4]
		add		eax, 0x10
		mov		ebx, dword ptr [eax]
		mov		tmpCoord.x,ebx
		mov		ebx, dword ptr [eax + 4]
		mov		tmpCoord.y,ebx
		mov		ebx, dword ptr [eax + 8]
		mov		tmpCoord.z, ebx
		popad
	}
	return tmpCoord;
}
void __stdcall SetPlayerAngle(DWORD pBase, float angle)
{
	__asm
	{
		pushad
		mov		eax,	pBase
		mov		eax, dword ptr [eax+0xe4]
		add		eax, 0x20
		mov		ebx,angle
		mov		dword ptr [eax], ebx
		popad
	}
}
void InitAddress()
{
	HMODULE hMain = GetModuleHandle(NULL);
	ULONG ulWowBase = (ULONG)hMain;

	GetGuidCall = ulWowBase + player_guid_call_offest;
	pGetBaseCall = ulWowBase + player_base_call_offest;
	pGetBasethis = ulWowBase + player_base_this_offest;

}


OBJECTGUID tmpguid;
DWORD playerBase;
COORDINATE tmpcoord;


BOOL WINAPI DllMain(
						HANDLE hDllHandle, 
						DWORD dwReason, 
						LPVOID lpreserved 
						)
{
	switch (dwReason)
	{
	case DLL_PROCESS_ATTACH:
		InitAddress();
		tmpguid = GetPlayerGUID();
		playerBase = GetPlayerBase(tmpguid);
		MsgOutW(_T("PlayerBase = 0x%08x\n"),playerBase);
		tmpcoord = GetPlayerCoord(playerBase);
		MsgOutW(_T("x=%f,y=%f,z=%f\n"),tmpcoord.x,tmpcoord.y,tmpcoord.z);
		break;
	case DLL_PROCESS_DETACH:
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	}
	return TRUE;
}


void MsgOutW(const TCHAR* format,...)
{
	va_list x;
	TCHAR strMsg[256];
	ZeroMemory(strMsg,256);
	va_start(x,format);
	//	va_arg(x,int); 
	_vstprintf_s(strMsg,format,x);
	OutputDebugString(strMsg);
	va_end(x);
}